import 'package:get/get.dart';
import 'package:high_help_apps/common/apis/v1/commodity.dart';
import 'package:high_help_apps/common/entities/life_help/commodity_info_request_entity.dart';
import 'package:high_help_apps/common/routes/app_pages.dart';

import 'second_hand_market_state.dart';

class SecondHandMarketLogic extends GetxController {

  final state = SecondHandMarketState();
  bool isInit = false;

  // 他是页面一开始加载后，会自动调用的方法
  @override
  void onReady() async {
    super.onReady();
    await firstLoadCommodityInfo();
  }


  Future firstLoadCommodityInfo() async {
    await loadCommodityInfo();
    isInit = true;
  }

  Future loadCommodityInfo() async  {
    CommodityInfoRequestEntity requestEntity = CommodityInfoRequestEntity().fromJson({
      'title': '',
      'current': 1,
      'size': 20
    });
    state.commodityInfoEntity = await CommodityAPI.getCommodityList(requestEntity);
    // 刷新组件的
    update();
  }

  /// 跳转到二手商品详情页面
  void redirectIntoCommodityInfo(int index) {
    Get.toNamed(AppRoutes.COMMODITY_DETAIL, arguments: {
      'record': state.commodityInfoEntity.records[index]
    });
  }
}
